package cms.simmytech.service;
import cms.simmytech.mapper.TbMajorMapper;
import cms.simmytech.model.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class TbMajorService {
    @Autowired
    private TbMajorMapper mapper;
    //直接返回整个专业列表
    public PageInfo<TbMajor> getMajorList(int pageNum, int pageSize, String name, Byte majorType){
        PageHelper.startPage(pageNum, pageSize);
        TbMajorExample example=new TbMajorExample();
        TbMajorExample.Criteria criteria = example.createCriteria();
        if(!"".equals(name)&&null!=name){
            criteria.andMajorNameLike("%"+name+"%")	;
        }
        if(majorType!=null&&majorType!=0){
            criteria.andMajorTypeEqualTo(majorType);
        }
        List<TbMajor> list= mapper.selectByExample(example);
        PageInfo<TbMajor> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //根据专业名称模糊查询，得到list<TbMajor>专业列表，专业列表放进paeInfo
    public PageInfo<TbMajor> getMajorListByName(String majorName){
        TbMajorExample example=new TbMajorExample();
        TbMajorExample.Criteria criteria = example.createCriteria();
        criteria.andMajorNameLike("%"+majorName+"%");//通过专业名称进行模糊查询，不要忘记%%
        List<TbMajor> list=null;
        try{
            list=mapper.selectByExample(example);
        }catch(Exception e){
            e.printStackTrace();
        }
        PageInfo<TbMajor> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //新增或者修改专业信息
    public void saveMajor(TbMajor major) {
        if (major.getMajorId()== null) {//只能根据major的id来判断
            mapper.insertSelective(major);//往表里添加信息
        } else {
            mapper.updateByPrimaryKeySelective(major);//往表里更新信息
        }
    }
    //增加专业
    public Boolean addMajor(TbMajor major){
        if(major!=null){
            try{
                mapper.insert(major);
                return true;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }
    //删除
    public Boolean deleteMajorById(Integer id){
        try{
            mapper.deleteByPrimaryKey(id);
            return true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
    //查询一个专业的信息
    public TbMajor findMajorById(Integer id) {
        TbMajor result=new TbMajor();
        try{
            result=mapper.selectByPrimaryKey(id);
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }
    //返回所有专业的信息
    public List<TbMajor> findAllMajor(){
        TbMajorExample example=new TbMajorExample();
        TbMajorExample.Criteria criteria = example.createCriteria();
        List<TbMajor> list=new ArrayList<>();
        try{
            list=mapper.selectByExample(example);
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }
    //修改专业
    public Boolean updataMajor(TbMajor major){
        if(major!=null){
            try{
                mapper.updateByPrimaryKeySelective(major);
                return true;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean checkCode(Integer id, String code, Integer type) {
        boolean res=true;
        if(id==null){
            TbMajor bean= mapper.findByCode(code,type);
            if(bean!=null){
                return false;
            }
        }else{
            TbMajor bean= mapper.findByCode(code,type);
            if(bean!=null&&bean.getMajorId()!=id){
                return false;
            }
        }
        return res;
    }
}
